package homework.Map;

import java.util.*;

/**
 * 2. 给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，
 * 然后使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，最后统计出各字符串元素的出现次数。
 */
public class One {

    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap();
        String[] str = new String[]{"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        List<String> list = Arrays.asList(str);

        //list转为set去重
        Set<String> set = new HashSet();
        set.addAll(list);
//        System.out.println(set);
        int n = 0;
        for (String obj : set) {
            for (int j = 0; j < list.size(); j++) {
                if (obj.equals(list.get(j))) {
                    n++;
                }
            }
            map.put(obj,n);
            n=0;
        }
        Set set1 = map.keySet();
        for (Object key : set1) {
            System.out.println(key + ":" + map.get(key));
        }

    }
}
